using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using JNPF.Basics.Models;
using JNPF.Basics.Logic;
using JNPF.Utils;
using System.Threading.Tasks;
using System.Linq;
using JNPF.Utils.Controller;
using JNPF.Basics.Logic.Common.Login;
using JNPF.Utils.Extensions;

namespace JNPF.Basics.Controller
{
	[Route("api/Jiushi/[Controller]")]
    public class CustomeractualcontrollerworkexperienceController : BaseApiController
    {
        private CustomeractualcontrollerworkexperienceBll CustomeractualcontrollerworkexperienceService=new CustomeractualcontrollerworkexperienceBll();
        private BillRuleBll billRuleService = new BillRuleBll();
        private DataInterfaceBll dataInterfaceBll = new DataInterfaceBll();
        private UserBll userBll = new UserBll();
        private OrganizeBll organizeBll = new OrganizeBll();


		#region GET

		/// <summary>
		/// 获取信息
		/// </summary>
		/// <param name="id">主键id</param>
		/// <returns></returns>
		[HttpGet("{id}")]
		public async Task<ResponseResult> GetInfo(int id)
		{
			var data = await CustomeractualcontrollerworkexperienceService.GetInfo(id);
			var output = data.MapTo<CustomeractualcontrollerworkexperienceUpOutput>();
			return Success(output);
		}

		/// <summary>
		/// 获取列表
		/// </summary>
		/// <param name="input">请求参数</param>
		/// <returns></returns>
		[HttpGet]
		public async Task<ResponseResult> GetList(CustomeractualcontrollerworkexperienceListQueryInput input)
		{
			var data = await CustomeractualcontrollerworkexperienceService.GetPageList(input);
			var outputList = data.list.MapToList<CustomeractualcontrollerworkexperienceListOutput>();
			 
			var output = new PageResult()
			{
				list = outputList,
				pagination = data.pagination
			};
			return Success(output);
		}

		#endregion

		#region POST

		/// <summary>
		/// 新建
		/// </summary>
		/// <param name="input">新建输入</param>
		/// <returns></returns>
		[HttpPost]
		public async Task<ResponseResult> Create([FromBody] CustomeractualcontrollerworkexperienceCrInput input)
		{
			var entity = input.MapTo<CustomeractualcontrollerworkexperienceEntity>();
			await CustomeractualcontrollerworkexperienceService.Create(entity);
			return Success("新建成功");
		}

		/// <summary>
		/// 导出
		/// </summary>
		/// <returns></returns>
		[HttpPost("Actions/Export")]
		public async Task<ResponseResult> Export([FromBody] CustomeractualcontrollerworkexperienceListQueryInput input)
		{
			var exportData = new List<CustomeractualcontrollerworkexperienceEntity>();
			if (input.dataType == 0)
			{
				var data = await CustomeractualcontrollerworkexperienceService.GetPageList(input);
				exportData = data.list;
			}
			else {
				exportData= await CustomeractualcontrollerworkexperienceService.GetList(input);
			}
			List<ParamsModel> paramList = "[{\"value\":\"工作时间\",\"field\":\"workTimePeriod\"},{\"value\":\"工作单位\",\"field\":\"employerUnit\"},{\"value\":\"工作岗位\",\"field\":\"position\"},{\"value\":\"工作内容\",\"field\":\"jobDescription\"}]".ToList<ParamsModel>();
			var userinfo = UserProvider.Instance.Get();
			ExcelConfig excelconfig = new ExcelConfig();
			excelconfig.FileName = "企业实际控制人工作经历.xls";
			excelconfig.HeadFont = "微软雅黑";
			excelconfig.HeadPoint = 10;
			excelconfig.IsAllSizeColumn = true;
			excelconfig.ColumnModel = new List<ExcelColumnModel>();
			List<string> selectKeyList = input.selectKey.Split(',').ToList<string>();
			foreach (var item in selectKeyList)
			{
				var isExist = paramList.Find(p => p.field == item);
				if (isExist != null)
				{
					excelconfig.ColumnModel.Add(new ExcelColumnModel() { Column = isExist.field.UpperFirstChar(), ExcelColumn = isExist.value });
				}
			}
			var addPath = ConfigurationKey.TemporaryFilePath + excelconfig.FileName;
			ExcelExportHelper<CustomeractualcontrollerworkexperienceEntity>.Export(exportData, excelconfig, addPath);
			return Success(new { name = excelconfig.FileName, url = "/api/Common/Download?encryption=" + userinfo.Id + "|" + excelconfig.FileName });
		}
		/// <summary>
		/// 编辑
		/// </summary>
		/// <param name="id">主键id</param>
		/// <param name="input">编辑输入</param>
		/// <returns></returns>
		[HttpPut("{id}")]
		public async Task<ResponseResult> Update(int id, [FromBody] CustomeractualcontrollerworkexperienceCrInput input)
		{
			var entity = input.MapTo<CustomeractualcontrollerworkexperienceEntity>();
			var isOk = await CustomeractualcontrollerworkexperienceService.Update(id, entity);
			return isOk > 0 ? Success("修改成功") : Error("修改失败");
		}

		/// <summary>
		/// 删除接口
		/// </summary>
		/// <param name="id">主键id</param>
		/// <returns></returns>
		[HttpDelete("{id}")]
		public async Task<ResponseResult> Delete(int id)
		{
			var entity = await CustomeractualcontrollerworkexperienceService.GetInfo(id);
			var isOk = await CustomeractualcontrollerworkexperienceService.Delete(entity);
			return isOk > 0 ? Success("删除成功") : Error("删除失败");
		}

		#endregion

	}
}